<?php
/**
 * @authors alex (20779512@qq.com)
 * @date    2014-03-24 17:25:39
 * @version 1.0
 */

class OrderAction extends AdminAction {
    
    #用户组列表
    public function index(){ 
        $userid = I('get.userid');
        $order_no = I('get.order_no');
        $min_money = I('get.min_money');
        $max_money = I('get.max_money');
        $startDate = I('get.startDate');
        $endDate = I('get.endDate');
        $payStatus = I('get.payStatus');

        if ($payStatus!='') {
            $map['payStatus'] = $payStatus;
        }

        if ($userid!='') {
            $map['memberID'] = $userid;
        }

        if ($order_no!='') {
            $map['order_no'] = $order_no;
        }

        if ($min_money!='' && $max_money!='') {
            $map['money'] = array('between',$min_money.','.$max_money);
        }

        if ($startDate!='' && $endDate!='') {
            $map['createTime'] = array('between',strtotime($startDate).','.strtotime($endDate));
        }


        $obj = M('Order');
        $count = $obj->where($map)->count();
        import("@.ORG.Page");
        $page = new Page($count, 20);
        $show = $page->show();
        $list = $obj->where($map)->order('id desc')->limit($page->firstRow . ',' . $page->listRows)->select();
        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->assign('userid', $userid);
        $this->display();
    }

    #查看订单
    function view() {

        $id = (int) $_GET['id'];
        $map['id']=$id;
        $obj = M('Order');
        $list = $obj->where("id=$id")->find();
        if (!$list) {
            $this->error('信息不存在');
        } else {
            unset($map);
            $map['order_no'] = $list['order_no'];
            $shop = M('OrderDetail')->where($map)->select();
            $this->assign('list', $list);
            $this->assign('shop', $shop);
            $this->display();
        }

    }


    #更新订单
    public function update(){
        $data['status'] = I('post.status');
        if ($data['status']=='') {
            $data['status']=0;
        }else{
            $data['status']=1;
        }
        $data['remark'] = I('post.remark');
        $data['backInfo'] = I('post.backInfo');
        $data['updateTime'] = time();
        $id= I('post.id');
        $list = M('Order')->where('id='.$id)->save($data);
        if ($list) {
            $state = 'SUCCESS';
            $message = '操作成功';
            $url = U('Order/index');
        }else{
            $state = '操作失败';
            $url = U('Order/index');
        }
        echo $this->echo_json_str($state,$message,$url);
    }

    public function change() {
        $state = I('get.state');
        $map['id']=I('get.id');
        $data['status']=$state;
        M('Order')->where($map)->save($data);
        redirect ($_SERVER['HTTP_REFERER']);
    }


    #删除废弃订单
    function del() {

        $id=I('post.selectedids');
        if($id==''){
            $state = "您没有选择任何信息！";
        }else{
            foreach ($id as $v) {
                $obj = M('Order');
                $where['id'] = $v;
                $where['payStatus'] = 0;
                $order_no = $obj->where($where)->getField('order_no');

                //删除订单详情
                M('OrderDetail')->where('order_no="'.$order_no.'"')->delete();

                //删除订单
                $obj->where($where)->delete();
            }
            $state = 'SUCCESS';
            $message = '删除成功';
            $url = U('Order/index');
        }
        echo $this->echo_json_str($state,$message,$url);
    }

    //订单明细
    public function detail(){     

        $proName = I('get.proName');
        $startDate = I('get.startDate');
        $endDate = I('get.endDate');

        if ($proName!='') {
            $map['pname'] = array('like','%'.$proName.'%');
        }


        if ($startDate!='' && $endDate!='') {
            $map['createTime'] = array('between',strtotime($startDate).','.strtotime($endDate));
        }   

        $obj = M('OrderDetail');
        $count = $obj->where($map)->count();
        import("@.ORG.Page");
        $page = new Page($count, 20);
        $show = $page->show();
        $list = $obj->where($map)->order('id desc')->limit($page->firstRow . ',' . $page->listRows)->select();
        foreach ($list as $key => $value) {
            $info = M('Order')->where('order_no="'.$value['order_no'].'"')->find();
            $list[$key]['name'] = $info['name'];
            $list[$key]['mobile'] = $info['mobile'];
            $list[$key]['address'] = $info['address'];
            $list[$key]['payStatus'] = $info['payStatus'];
        }
        $this->assign('list', $list);
        $this->assign('page', $show);
        $this->assign('proName', $proName);
        $this->display();
    }
}